﻿/*---------------------------------------------------------------------*/
// fileName="FileWatchCommon.cs" CompanyName="SureKAM"
// Copyright SureKAM (c). All right reserved.
/*---------------------------------------------------------------------*/

namespace SureKAM.BackUp.Notify.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    /// <summary>
    /// 文件监控类
    /// </summary>
    public class FileWatchCommon
    {
        #region 私有字段
        private string path;
        private long realSize;
        private DateTime lastTime;
        private Domain.Directory directory;
        private FileSystemWatcher fileSystemWatcher;
        #endregion

        #region 构造
        public FileWatchCommon(Domain.Directory directory)
        {
            this.path = directory.Address;
            this.directory = directory;
            this.fileSystemWatcher = new FileSystemWatcher();
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 判断目录是否存在
        /// </summary>
        /// <returns></returns>
        private bool Exists()
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(this.path);
            bool operation = false;
            try
            {
                if (directoryInfo.Exists)
                {
                    this.realSize = FileCommon.SummaryFilesSize(directoryInfo);
                    this.directory.RealSize = Convert.ToInt32(this.realSize / (1024 * 1024));
                    operation = true;
                }
            }
            catch (Exception)
            {
                operation = false;
            }
            return operation;
        }

        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            if (this.directory != null)
            {
                LogCommon.Write("[" + this.directory.BackUpServer + "]<" + DateTime.Now.ToString() + "><Edit>" + e.FullPath);
                this.directory.RealSize = Convert.ToInt32(this.realSize / (1024*1024));
                this.directory.LastChangeTime = DateTime.Now;
            }
        }

        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
        {
            if (this.directory != null)
            {
                LogCommon.Write("[" + this.directory.BackUpServer + "]<" + DateTime.Now.ToString() + "><Create>" + e.FullPath);
                this.directory.RealSize = Convert.ToInt32(this.realSize / (1024 * 1024));
                this.directory.LastChangeTime = DateTime.Now;
            }
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
        {
            if (this.directory != null)
            {
                LogCommon.Write("[" + this.directory.BackUpServer + "]<" + DateTime.Now.ToString() + "><Delete>" + e.FullPath);
                this.directory.RealSize = Convert.ToInt32(this.realSize / (1024 * 1024));
                this.directory.LastChangeTime = DateTime.Now;
            }
        }
        #endregion

        #region 属性
        /// <summary>
        /// 监控路径
        /// </summary>
        public string Path
        {
            get { return this.path; }
        }

        /// <summary>
        /// 占用空间
        /// </summary>
        public long RealSize
        {
            get { return this.realSize; }
        }

        /// <summary>
        /// 最后修改时间
        /// </summary>
        public DateTime LastTime
        {
            get { return this.lastTime; }
        }

        /// <summary>
        /// 文件目录
        /// </summary>
        public Domain.Directory FileDirectory
        {
            get { return this.directory; }
        }
        #endregion

        #region 公有方法
        /// <summary>
        /// 监控
        /// </summary>
        public void Monitoring()
        {
            if (Exists())
            {
                this.fileSystemWatcher.Path = this.path;
                this.fileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName |
                                                  NotifyFilters.DirectoryName;
                this.fileSystemWatcher.Changed += new FileSystemEventHandler(fileSystemWatcher_Changed);
                this.fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Created);
                this.fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemWatcher_Deleted);
                this.fileSystemWatcher.IncludeSubdirectories = true;
                this.fileSystemWatcher.EnableRaisingEvents = true;
            }
        }

        /// <summary>
        /// 停止监视
        /// </summary>
        public void ShortMonit()
        {
            this.fileSystemWatcher.EnableRaisingEvents = false;
        }
        #endregion

    }
}
